<?php

namespace App\Model;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\User
 *
 * @property integer $id
 * @property string $open_id
 * @property string $number 学生： 学号 老师：工号
 * @property string $name 姓名
 * @property integer $gender 0-女 1-男
 * @property string $password 密码
 * @property integer $role 0- 学生 1- 老师
 * @property string $college 学院
 * @property string $class 班级
 * @property string $major 专业
 * @property string $area 地区
 * @property string $home_address 家庭住址
 * @property string $dormitory_address 宿舍地址
 * @property string $high_school 高中
 * @property string $cellphone 手机号码
 * @property string $short_phone 短号
 * @property string $birthday 生日
 * @property string $identity_number
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property string $nickname
 * @property string $avatar
 * @property float $weight
 * @property string $subscribed_at
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereOpenId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereNumber($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereGender($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereRole($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereCollege($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereClass($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereMajor($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereArea($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereHomeAddress($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereDormitoryAddress($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereHighSchool($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereCellphone($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereShortPhone($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereBirthday($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereIdentityNumber($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereNickname($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereAvatar($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereWeight($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Model\User whereSubscribedAt($value)
 * @mixin \Eloquent
 */
class User extends Authenticatable
{

    use SoftDeletes;

    protected $table = 'user';

    protected $hidden = ['password', 'high_school', 'identity_number', 'home_address'];

    protected $guarded = [];

    protected $dates = ['deleted_at'];
    

    public function admin()
    {
        return $this->belongsTo('App\Model\Admin', 'number', 'number');
    }

    /**
     * 判断用户是否存在权限
     *
     * @param $name
     * @return bool
     */
    public function havePermission($name)
    {
        $admin = $this->admin;

        if(!$admin){
            return false;
        }
        $permission = Permission::where('name', $name)->first();

        $permissionRoles = PermissionRole::where('permission_id', $permission->id)->get()->pluck('role_id')->all();

        $roleUsers = RoleUser::where('admin_id', $admin->id)->get()->pluck('role_id')->all();

        return array_intersect($permissionRoles, $roleUsers) ? true : false;

    }

    /**
     * 判断用户是否存在角色
     *
     * @param $name
     * @return bool
     */
    public function haveRole($name)
    {
        $admin = $this->admin;

        if(!$admin){
            return false;
        }

        $role = Role::where('name', $name)->first();

        return RoleUser::where('admin_id', $admin->id)->where('role_id', $role->id)->first() ? true : false;
    }

    public function class()
    {
        return $this->belongsTo(Classes::class);
    }
}
